package xhr;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

public class Producer {
    public static void main(String[] args) throws Exception {

        // 创建生产者
        DefaultMQProducer defaultMQProducer = new DefaultMQProducer("producer_hello_group");
        // 使用nameserver 负载均衡
        defaultMQProducer.setNamesrvAddr("127.0.0.1:9876");
        // 启动生产者
        defaultMQProducer.start();


        // 设置消费者主题
        Message message = new Message("hello_topic2", "hello_tags2", "我在源码学习Java！".getBytes(StandardCharsets.UTF_8));
        // 发送消息 发送异步消息
        defaultMQProducer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println(sendResult);
            }

            @Override
            public void onException(Throwable throwable) {
                throwable.printStackTrace();
                System.out.println("发送消息失败");
            }
        });


    }
}
